﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class borrowerInfoEdit : Form
    {
        public borrowerInfoEdit(string str)
        {
            InitializeComponent();
            this.txtBorrowerId.Text = str;
        }


        private string editBorrowerId;
        private void borrowerInfoEdit_Load(object sender, EventArgs e)
        {
            editBorrowerId = this.txtBorrowerId.Text;
            this.dtpLineDate.Enabled = false;
            this.cboSex.Items.Add("男");
            this.cboSex.Items.Add("女");
            this.cboSex.SelectedIndex = 0;
            this.cboCardState.Items.Add("有效");
            this.cboCardState.Items.Add("挂失");
            this.cboCardState.Items.Add("停用");
            this.cboCardState.SelectedIndex = 0;
            DataTable dt = bibliothecaClass.getBorrowerType();
            this.cboBorrowerType.DataSource = dt;
            this.cboBorrowerType.DisplayMember = "借阅者类型";
            DataTable dt1 = bibliothecaClass.getDepartmentName();
            this.cboDepartment.DataSource = dt1;
            this.cboDepartment.DisplayMember = "部门名称";
            DataTable dt2 = bibliothecaClass.getBorrowerInfor(editBorrowerId);
            if (dt2.Rows.Count != 0)
            {
                this.txtBorrowerId.Text = dt2.Rows[0]["借阅者编号"].ToString().Trim();
                this.txtBorrowerName.Text = dt2.Rows[0]["姓名"].ToString().Trim();
                this.cboSex.Text = dt2.Rows[0]["性别"].ToString().Trim();
                this.dtpBorthday.Text = dt2.Rows[0]["出生日期"].ToString().Trim();
                this.cboBorrowerType.Text = dt2.Rows[0]["借阅者类型"].ToString().Trim();
                this.cboCardState.Text = dt2.Rows[0]["借书证状态"].ToString().Trim();
                this.dtpCardDate.Text = dt2.Rows[0]["办证日期"].ToString().Trim();
                this.dtpLineDate.Text = dt2.Rows[0]["有效期截止日期"].ToString().Trim();
                this.txtCardId.Text = dt2.Rows[0]["证件号码"].ToString().Trim();
                this.txtTel.Text = dt2.Rows[0]["联系电话"].ToString().Trim();
                this.cboDepartment.Text = dt2.Rows[0]["部门名称"].ToString().Trim();
                this.txtLeftCash.Text = dt2.Rows[0]["押金剩余"].ToString().Trim();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(this.txtBorrowerId.Text=="")
            {
                MessageBox.Show("借阅者编号不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtBorrowerId.Focus();
            }
            else if(this.txtBorrowerName.Text=="")
            {
               MessageBox.Show("姓名不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtBorrowerName.Focus();
            }
            else if(this.cboSex.Text=="")
            {
               MessageBox.Show("性别不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.cboSex.Focus();
            }
           else if(this.dtpBorthday.Text=="")
            {
               MessageBox.Show("出生日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.dtpBorthday.Focus();
            }
           else if(this.cboBorrowerType.Text=="")
            {
               MessageBox.Show("借阅者类型不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.cboBorrowerType.Focus();
            }
           else if(this.cboCardState.Text=="")
            {
               MessageBox.Show("借书证状态不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.cboCardState.Focus();
            }
            else if(this.dtpCardDate.Text=="")
            {
               MessageBox.Show("办证日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.dtpCardDate.Focus();
            }
            else if(this.dtpLineDate.Text=="")
            {
               MessageBox.Show("有效期截止日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.dtpLineDate.Focus();
            }
            else if(this.txtCardId.Text=="")
            {
               MessageBox.Show("证件卡号不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtCardId.Focus();
            }
            else if(this.txtTel.Text=="")
            {
               MessageBox.Show("联系电话不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtTel.Focus();
            }
            else if(this.cboDepartment.Text=="")
            {
               MessageBox.Show("部门不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.cboDepartment.Focus();
            }
            else
            {
                //DataTable dt = bibliothecaClass.getDepartmentId(this.cboDepartment.Text);
                //if (dt.Rows.Count != 0)
                //{
                //    departmentId = dt.Rows[0][0].ToString();
                //}
                if(this.txtBorrowerId.Text!=editBorrowerId)
                {
                    //dt.Clear();
                    DataTable dt = bibliothecaClass.getBorrowerInfor(this.txtBorrowerId.Text);
                    if (dt.Rows.Count == 0)
                    {
                        Boolean updateResult;
                        updateResult = bibliothecaClass.borrowerEditAll(txtBorrowerId.Text.ToString().Trim(), txtBorrowerName.Text.ToString().Trim(), cboSex.Text.ToString().Trim(), Convert.ToDateTime(dtpBorthday.Text.ToString().Trim()), borrowerType, cboCardState.Text.ToString().Trim(), Convert.ToDateTime(dtpCardDate.Text.ToString().Trim()), Convert.ToDateTime(dtpLineDate.Text.ToString().Trim()), txtCardId.Text.ToString().Trim(), txtTel.Text.ToString().Trim(), departmentId, float.Parse(txtLeftCash.Text.ToString().Trim()), editBorrowerId);
                        if (updateResult == true)
                        {
                            MessageBox.Show("借阅者信息修改成功！", "提示信息");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("借阅者信息修改失败，请重试！", "提示信息");
                            this.txtBorrowerId.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("此借阅者已存在,请重新输入！","提示信息");
                        this.txtBorrowerId.Clear();
                        this.txtBorrowerId.Focus();
                    }
                }
                else
                {
                    Boolean updateResult;
                    updateResult = bibliothecaClass.borrowerEditPart(txtBorrowerName.Text.ToString().Trim(), cboSex.Text.ToString().Trim(), Convert.ToDateTime(dtpBorthday.Text.ToString().Trim()), borrowerType, cboCardState.Text.ToString().Trim(), Convert.ToDateTime(dtpCardDate.Text.ToString().Trim()), Convert.ToDateTime(dtpLineDate.Text.ToString().Trim()), txtCardId.Text.ToString().Trim(), txtTel.Text.ToString().Trim(), departmentId, float.Parse(txtLeftCash.Text.ToString().Trim()),txtBorrowerId.Text.ToString().Trim());
                    if (updateResult == true)
                    {
                        MessageBox.Show("借阅者信息修改成功！", "提示信息");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("借阅者信息修改失败，请重试！", "提示信息");
                        this.txtBorrowerId.Focus();
                    }
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private int borrowerType;
        private void cboBorrowerType_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getBorrowerTypeInfo(this.cboBorrowerType.Text.ToString().Trim());
            if (dt.Rows.Count != 0)
            {
                borrowerType=Convert.ToInt32(dt.Rows[0][0].ToString());
            }
        }


        private string departmentId;
        private void cboDepartment_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getDepartmentId(this.cboDepartment.Text.ToString().Trim());
            if(dt.Rows.Count!=0)
            {
                departmentId=dt.Rows[0][0].ToString();
            }
        }

        private void txtTel_TextChanged(object sender, EventArgs e)
        {
            if (this.txtTel.Text.Trim() != "")
            {
                try
                {
                    int i =int.Parse(this.txtTel.Text.ToString().Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("电话号码只能为数字型数据！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void txtLeftCash_TextChanged(object sender, EventArgs e)
        {
            if (this.txtTel.Text.Trim() != "")
            {
                try
                {
                    float leftCash = float.Parse(this.txtLeftCash.Text.ToString().Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("押金剩余只能为数字型数据！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}